﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using QLTuyenXe.DAL;
using QLTuyenXe.DAO;
using QLTuyenXe.library;
using System.Data.SqlClient;

namespace QLTuyenXe.BLL
{
    class BKhachHang
    {
        MyConnectUnit myConnU = new MyConnectUnit();

        //kiem tra dk trc khi xóa(Mr.Tuyen)
        public DataTable selectDKXoa(String dk)
        {
            DataTable tb = new DataTable();
            tb = myConnU.Select("HOP_DONG", "KHACH_HANG", "HOP_DONG.maKH", "KHACH_HANG.maKH","KHACH_HANG.maKH='"+ dk +"'");
            return tb;
        }



        //=============================================================================================
        //Hàm lấy giá trị lơn nhất (Mr.Tuyên K_54HTTT)
        public String getOrder()
        {
            string strID = myConnU.GetTopDataCell("KHACH_HANG", "maKH");
            string strID1;
            string strID2;
            strID1 = strID.Substring(0, 2);
            strID2 = strID.Substring(2, 5);
            int autoNum = Convert.ToInt32(strID2) + 1;

            if (autoNum < 10)
                return strID1 + "0000" + autoNum;

            else if (autoNum >= 10 && autoNum < 100)
                return strID1 + "000" + autoNum;

            else if (autoNum >= 100 && autoNum < 1000)
                return strID1 + "00" + autoNum;
            else if (autoNum >= 1000 && autoNum < 10000)
                return strID1 + "0" + autoNum;
            else if (autoNum >= 10000 && autoNum < 100000)
                return strID1 + "" + autoNum;

            else
                return "";
        }
        //End hàm lấy giá trị lưn nhất (Mr.Tuyên K_54HTTT)


        public int Insert(KHACHHANG khachHang)
        {
            ColumnData maKH = new ColumnData("maKH", 1, khachHang.MaKH.ToString());
            ColumnData tenCT = new ColumnData("donviB", 1, khachHang.DonviB.ToString());
            ColumnData daiDien = new ColumnData("daidienB", 1, khachHang.DaidienB.ToString());
            ColumnData chuVu = new ColumnData("chucvuB", 1, khachHang.ChucvuB.ToString());
            ColumnData diaChi = new ColumnData("diachiB", 1, khachHang.DiachiB.ToString());
            ColumnData dienThoai = new ColumnData("dienthoaiB", 1, khachHang.DienthoaiB.ToString());
            ArrayList ob = new ArrayList();
            ob.Add(maKH);
            ob.Add(tenCT);
            ob.Add(daiDien);
            ob.Add(chuVu);
            ob.Add(diaChi);
            ob.Add(dienThoai);
            return myConnU.Insert("KHACH_HANG", ob);
        }

         public ArrayList Select(String columns, String condition, String order)
        {
            ArrayList khachHang = new ArrayList();
            DataTable tb = new DataTable();
            tb = myConnU.Select("KHACH_HANG", columns, condition, order);
            if(tb!= null)
                foreach (DataRow r in tb.Rows)
                {
                    KHACHHANG kh = new KHACHHANG();
                    kh.MaKH = r["maKH"].ToString();
                    kh.DonviB = r["donviB"].ToString();
                    kh.DaidienB = r["daidienB"].ToString();
                    kh.ChucvuB = r["chucvuB"].ToString();
                    kh.DiachiB = r["diachiB"].ToString();
                    kh.DienthoaiB = r["dienthoaiB"].ToString();
                    khachHang.Add(kh);
                }
            return khachHang;
        }

        public ArrayList Select(String columns, String condition)
        {
            return this.Select(columns, condition, null);
        }

       
        public ArrayList Select(String columns)
        {
            return this.Select(columns,  null);
        }
       
        public ArrayList Select()
        {
            return this.Select("*");
        }

        /// <summary>
        /// Tìm kiếm theo lựa chọn
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public ArrayList Select_TK(String condition)
        {
            ArrayList khachHang = new ArrayList();
            DataTable tb = new DataTable();
            tb = myConnU.Select("KHACH_HANG","*",condition);
            if (tb != null)
                foreach (DataRow r in tb.Rows)
                {
                    KHACHHANG kh = new KHACHHANG();
                    kh.MaKH = r["maKH"].ToString();
                    kh.DonviB = r["donviB"].ToString();
                    kh.DaidienB = r["daidienB"].ToString();
                    kh.ChucvuB = r["chucvuB"].ToString();
                    kh.DiachiB = r["diachiB"].ToString();
                    kh.DienthoaiB = r["dienthoaiB"].ToString();
                    khachHang.Add(kh);
                }
            return khachHang;
        }

        public DataTable Select_DK(String condition1, string condition2, string condition3, string condition4)
        {
            //ArrayList ar = new ArrayList();
            DataTable tb = myConnU.Select_KiemtraKH("KHACH_HANG", "donviB=N'" + condition1 + "'", "daidienB=N'"+ condition2 +"'" ,"diachiB=N'" + condition3 + "'", "dienthoaiB=N'" + condition4 + "'");
            return tb;
        }

        public int Update(KHACHHANG khachHang, string condition)
        {
            //ColumnData maKH = new ColumnData("maKH", 1, khachHang.MaKH.ToString());
            ColumnData tenCT = new ColumnData("donviB", 1, khachHang.DonviB.ToString());
            ColumnData daiDien = new ColumnData("daidienB", 1, khachHang.DaidienB.ToString());
            ColumnData chuVu = new ColumnData("chucvuB", 1, khachHang.ChucvuB.ToString());
            ColumnData diaChi = new ColumnData("diachiB", 1, khachHang.DiachiB.ToString());
            ColumnData dienThoai = new ColumnData("dienthoaiB", 1, khachHang.DienthoaiB.ToString());
            ArrayList ob = new ArrayList();
            //ob.Add(maKH);
            ob.Add(tenCT);
            ob.Add(daiDien);
            ob.Add(chuVu);
            ob.Add(diaChi);
            ob.Add(dienThoai);
            return myConnU.Update("KHACH_HANG", ob, condition);
        }
        public int Update(ArrayList khachHang, String condition)
        {
            int count = 0;
            foreach(KHACHHANG  kh in khachHang)
            {
                count += this.Update(kh, condition);
            }
            return count;
        }

        public int Delete(String condition)
        {
            return myConnU.Delete("KHACH_HANG", condition);
        }
    }
}
